package sns.action;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.Relation;
import sns.bean.User;
import sns.dao.RelationDao;
import sns.dao.UserDao;
import sns.service.ImpressionService;
import sns.service.RelationService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class CancelFollowAction extends ActionSupport {

	// injection
	private RelationService relationService;
	private RelationDao relationDao;
	private UserDao userDao;
	private ImpressionService impressionService;
	// parameter
	private Relation relation;
	private User currentUser;
	private User visitingUser;
	// value pass in
	private Long currentUserId;
	private Long visitingUserId;
	// return value
	private Long visitId;
	
	
	@Transactional
	public String execute() {

		System.out.println("@@@@@@@@@@@@@@@@@@@@IN CancelFollowAction");
		// check if already followed
		if (currentUserId == null || visitingUserId == null) {
			System.out.println("Lack of id.");
			return ActionSupport.INPUT;
		}
		currentUser = userDao.findUserById(currentUserId);
		visitingUser = userDao.findUserById(visitingUserId);
		visitId = visitingUser.getIduser();

		if (!relationService.checkIfFollow(currentUser, visitingUser)) {
			return "NOT_FOLLOWED";
		} else {
			// TODO I think it should be a new function in finding relation
			relation = relationService.getRelation(currentUser, visitingUser);
			// relation.setFollowtime(new Date());
			impressionService.deleteImpressionByRelation(relation);
			relationDao.delete(relation);
		}
		return Action.SUCCESS;
	}

	public Long getVisitId() {
		return visitId;
	}

	public void setRelationService(RelationService relationService) {
		this.relationService = relationService;
	}

	public void setRelationDao(RelationDao relationDao) {
		this.relationDao = relationDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public void setCurrentUserId(Long currentUserId) {
		this.currentUserId = currentUserId;
	}

	public void setVisitingUserId(Long visitingUserId) {
		this.visitingUserId = visitingUserId;
	}

	public void setImpressionService(ImpressionService impressionService) {
		this.impressionService = impressionService;
	}

}
